/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.tech15.core.action;

import static javacommon.util.extjs.Struts2JsonHelper.outXmlJson;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.LoginInfo;
import javacommon.util.Notes;
import javacommon.util.ResInfo;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.tech15.core.model.ModuleAuth;
import com.tech15.core.service.ModuleAuthManager;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


public class ModuleAuthAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String SHOW_JSP = "/sys/modulePermission_edit.jsp";
	
	private ModuleAuthManager moduleAuthManager;
	
	private ModuleAuth moduleAuth;
	Long id = null;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			moduleAuth = new ModuleAuth();
		} else {
			moduleAuth = (ModuleAuth)moduleAuthManager.getById(id);
		}
	}
	
	public void setModuleAuthManager(ModuleAuthManager manager) {
		this.moduleAuthManager = manager;
	}	
	
	public Object getModel() {
		return moduleAuth;
	}
	
	public void setMaid(Long val) {
		this.id = val;
	}

	/** 查看对象*/
	public String show() throws UnsupportedEncodingException {
		String idName = URLDecoder.decode(modIdName,"UTF-8");
		moduleAuth.setMdid(new Long(idName.substring(0,idName.indexOf("|"))));
		moduleAuth.setModName(idName.substring(idName.indexOf("|")+1));
		moduleAuthManager.show(moduleAuth);
		return SHOW_JSP;
	}
	
	/** 保存新增对象 */
	public void auth() {
		Map<String, Object> resInfo = null;
		try {
			LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
			moduleAuthManager.auth(moduleAuth);
			resInfo = ResInfo.getResInfo("", false, false, 2, "操作成功", "");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}	
		outXmlJson(resInfo);
	}
	/**设置用户权限*/
	public void perUser(){
		Map<String, Object> resInfo = null;
		try {
			LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
			moduleAuthManager.perAuth(chkper,userId,"1");
			resInfo = ResInfo.getResInfo("", false, false, 2, "操作成功", "");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}	
		outXmlJson(resInfo);
	}
	/**设置部门权限*/
	public void perDept(){
		Map<String, Object> resInfo = null;
		try {
			LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
			moduleAuthManager.perAuth(chkper,deptId,"2");
			resInfo = ResInfo.getResInfo("", false, false, 2, "操作成功", "");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}	
		outXmlJson(resInfo);
	}
	/**设置组权限*/
	public void perGroup(){
		Map<String, Object> resInfo = null;
		try {
			LoginInfo loginInfo = (LoginInfo) getRequest().getSession().getAttribute(Notes.LOGIN_INFO);
			moduleAuthManager.perAuth(chkper,groupId,"2");
			resInfo = ResInfo.getResInfo("", false, false, 2, "操作成功", "");
		} catch (Exception e) {
			e.printStackTrace();
			resInfo = ResInfo.getResInfo("",false, false, 1, "操作失败","");
		}	
		outXmlJson(resInfo);
	}
	
	private String chkper;
	private String modIdName;
	
	private Long userId;
	private Long posId;
	private Long deptId;
	private Long groupId;

	public Long getDeptId() {
		return deptId;
	}

	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}

	public Long getGroupId() {
		return groupId;
	}

	public void setGroupId(Long groupId) {
		this.groupId = groupId;
	}

	public Long getPosId() {
		return posId;
	}

	public void setPosId(Long posId) {
		this.posId = posId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getChkper() {
		return chkper;
	}

	public void setChkper(String chkper) {
		this.chkper = chkper;
	}

	public String getModIdName() {
		return modIdName;
	}

	public void setModIdName(String modIdName) {
		this.modIdName = modIdName;
	}

}
